package com.facesteel.speech_play.utils

import android.content.Context
import android.media.AudioManager
import android.view.Gravity
import android.widget.Toast

class SpeakerUtil private constructor() {

    companion object {
        fun switchSpeaker(context: Context, enable: Boolean){
            if(SpeechPlayer.isBluetooth()){
                Toast.makeText(context, "当前为蓝牙播放", Toast.LENGTH_SHORT).show()
                return
            }

            val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
            audioManager.isSpeakerphoneOn = enable // true 为扬声器，false 为听筒
            audioManager.mode = if (enable) AudioManager.MODE_NORMAL else AudioManager.MODE_IN_COMMUNICATION
            val toast: Toast = Toast.makeText(context,
                if (enable) "扬声器播放" else "听筒播放,靠近耳朵聆听", Toast.LENGTH_SHORT)
            toast.setGravity(Gravity.TOP or Gravity.CENTER_HORIZONTAL, 0, 90)
            toast.show()

        }
    }

}